package com.UU.J2EE.PizzaStore.dao;

import java.sql.ResultSet;
import java.util.ArrayList;

import com.UU.J2EE.PizzaStore.beans.CustomerBean;
import com.UU.J2EE.PizzaStore.beans.MaterialBean;
import com.UU.J2EE.PizzaStore.beans.PizzaBean;

public class PizzaDao {
	public ArrayList<PizzaBean> getAllPizza()
	{
		ResultSet rs=null;
        boolean hasOrNot = false;
        ArrayList<PizzaBean> pizzalist = new ArrayList<PizzaBean>();
        try{
            
       
            String sql;
            sql ="SELECT * from pizza";
            rs= DBUtil.executeQuery(sql,null);

	    // analyze the result set
	    
	    hasOrNot = rs.next();
	    PizzaMaterialDao pizzaMaterialDao = new PizzaMaterialDao();
	    MenuDao menuDao = new MenuDao();
	    while(hasOrNot)
	    {
	    	PizzaBean pizzaBean = new PizzaBean();
	    	pizzaBean.setName(rs.getString("name"));
	    	pizzaBean.setId(rs.getInt("idpizza"));
	    	pizzaMaterialDao.getMaterialForPizza(pizzaBean);
	    	menuDao.getMenuForPizza(pizzaBean);
	    	pizzalist.add(pizzaBean);
	    	hasOrNot = rs.next();
	    }
	}   
	catch(Exception sqle){
            sqle.printStackTrace();
	}
        finally{
        	DBUtil.close(rs, DBUtil.getPs(), DBUtil.getCt());
        }
        return pizzalist;
  }

	public boolean getPizza(PizzaBean pizzaBean) {
        ResultSet rs=null;
        boolean hasOrNot = false;
        
        try{
            
       
            String sql;
            sql ="SELECT * from pizza where idpizza = " +
                     "'" + pizzaBean.getId() + "'";
            rs= DBUtil.executeQuery(sql,null);

	    // analyze the result set
	    
	    hasOrNot = rs.next();
	    if(hasOrNot)
	    {
	    	pizzaBean.setName(rs.getString("name"));
	    	PizzaMaterialDao pizzaMaterialDao = new PizzaMaterialDao();
	    	pizzaMaterialDao.getMaterialForPizza(pizzaBean);
	    }
	}   
	catch(Exception sqle){
            sqle.printStackTrace();
	}
        finally{
        	DBUtil.close(rs, DBUtil.getPs(), DBUtil.getCt());
        }
        return hasOrNot;
  }
}
